package com.vitabuybuy.tv.ui.mine.group

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentMyGroupBuyChildBinding
import com.vitabuybuy.tv.dto.TabEntity
import com.vitabuybuy.tv.ui.mine.group.adapter.MyLimitedGroupBuyListAdapter
import com.vitabuybuy.tv.ui.mine.group.viewmodel.MyGroupVieModel
import kotlinx.android.synthetic.main.fragment_my_group_buy_child.*

class LimitedFragment : BaseFragment<MyGroupVieModel, FragmentMyGroupBuyChildBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_my_group_buy_child

    private val mTabEntities = ArrayList<CustomTabEntity>()

    private val myLimitedGroupBuyListAdapter by lazy { MyLimitedGroupBuyListAdapter() }

    private var pageNumber = 1
    private var status = "participating"

    override fun initView(savedInstanceState: Bundle?) {
        mBinding?.mViewModel = viewModel
        mTabEntities.add(TabEntity(getString(R.string.participating), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.missed), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.won), 0, 0))
        mTabLayout_1.setTabData(mTabEntities)
        listView.apply {
            adapter = myLimitedGroupBuyListAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }
    override fun lazyLoadData() {
        viewModel.getOrderList(null, status, pageNumber)
    }

    override fun observeData() {
        viewModel.goodslist.observe(this, Observer {
            if (pageNumber == 1) {
                myLimitedGroupBuyListAdapter.setNewData(it)
            } else {
                myLimitedGroupBuyListAdapter.addData(it)
            }
            if (myLimitedGroupBuyListAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when(msg.code){
            1 ->refresh.onLoad(msg.arg1)
        }
    }

    override fun eventHandling() {
        mTabLayout_1.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                pageNumber = 1
                when (position) {
                    0 -> status = "participating"
                    1 -> status = "failure"
                    2 -> status = "gain"
                }
                viewModel.getOrderList(null, status, pageNumber)
            }

            override fun onTabReselect(position: Int) {
            }
        })
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getOrderList(null, status, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getOrderList(null, status, pageNumber)
            }
        })
        myLimitedGroupBuyListAdapter.setOnItemClickListener { _, item, _ ->
            MyGroupBuyDetailActivity.openActivity(mContext,item.id,2)
        }
    }
}
